﻿// ===============================================================================
// DotNet.Platform 开发框架 2016 版权所有
// ===============================================================================

using System;
using System.ComponentModel.DataAnnotations;
using DotNet.Extensions;
using DotNet.Helper;

namespace DotNet.DataAnnotationsExtensions
{
    /// <summary>
    /// 身份证验证
    /// </summary>
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class IdCardAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null) return ValidationResult.Success;
            var result = IDCardHelper.Validate(value.ToStringOrEmpty());
            return result.Success ? ValidationResult.Success : new ValidationResult(ErrorMessage ?? result.Message);
        }
    }
}